package cn.ekuma.android.ui.filter;

import java.text.SimpleDateFormat;
import java.util.Date;
import net.sourceforge.jtimepiece.TimeUtil;
import cn.ekuma.android.ui.calendar.DateWidget.OnSelectedDateListener;
import cn.ekuma.data.ui.android.R;
import cn.ekuma.epos.android.util.Utils;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

public class QuickTimeFilter extends LinearLayout{
	
	public final static int TIMERCYCLETYPE_Null=0;
    public final static int TIMERCYCLETYPE_Today=1;
    public final static int TIMERCYCLETYPE_Week=2;
    public final static int TIMERCYCLETYPE_ThreeWeek=3;
    public final static int TIMERCYCLETYPE_Month=4;
    
    private int timeCycleType=TIMERCYCLETYPE_Null;
    private java.util.Date startDate;
    private java.util.Date endDate;
    
    private Button btnStartTime,btnEndTime,btnSeach,btnPrivous,btnNext;
    private Spinner spnTimeCycle;
    
    public final static SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
    
    int[] msgIds = {R.string.timeCycleType_null,
    		        R.string.timeCycleType_Today,
    		        R.string.timeCycleType_Week,
    		        R.string.timeCycleType_ThreeWeek,
    		        R.string.timeCycleType_Month
    		 };
    
    int[] cycleValues={
    		TIMERCYCLETYPE_Null,
    		TIMERCYCLETYPE_Today,
    		TIMERCYCLETYPE_Week,
    		TIMERCYCLETYPE_ThreeWeek,
    		TIMERCYCLETYPE_Month
    };

    
	public QuickTimeFilter(Context context) {
		super(context);
		init(context);
	}
	
	public QuickTimeFilter(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}
	
	public void resertValues(){
		if(spnTimeCycle.getSelectedItemPosition()==0){
			startDate=endDate=null;
			timeCycleType=TIMERCYCLETYPE_Null;
			refushView();
		}else
		    spnTimeCycle.setSelection(0);
		
	}
	
	
    private void init(final Context context) {
    	// ��ȡҳ�沼�� 
        View mainView = LayoutInflater.from(context).inflate( 
                        R.layout.time_filter, this); 
        btnStartTime=(Button) mainView.findViewById(R.id.btnStartTime);
        
        btnStartTime.setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				Utils.popupDateWidget(context, btnStartTime,new OnSelectedDateListener(){
					public void OnSelectedDate(Date date) {
						startDate=date;
						refushView();
					}
				});
				
			}
        });
        
        btnEndTime=(Button) mainView.findViewById(R.id.btnEndTime);
        btnEndTime.setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				Utils.popupDateWidget(context, btnEndTime,new OnSelectedDateListener(){
					public void OnSelectedDate(Date date) {
						endDate=date;
						refushView();
					}
				});
			}
        });

        btnSeach=(Button) mainView.findViewById(R.id.btnSeach);
        btnSeach.setOnClickListener(new OnClickListener(){

			public void onClick(View v) {
				fireTimeCycleChanged();
			}
        	
        });
        
        btnPrivous=(Button) mainView.findViewById(R.id.btnPrivous);
        btnPrivous.setOnClickListener(new OnClickListener(){
			
			public void onClick(View v) {
				fireLastCycle();
			}
        	
        });
        
        btnNext=(Button) mainView.findViewById(R.id.btnNext);
        btnNext.setOnClickListener(new OnClickListener(){

			
			public void onClick(View v) {
				fireNextCycle();
			}
        	
        });
        
        spnTimeCycle=(Spinner) mainView.findViewById(R.id.spnTimeCycle);
        
        spnTimeCycle.setAdapter(
        	     new BaseAdapter() {

        	     	public int getCount() {
        	     	return msgIds.length;
        	     	}

        	     	public Object getItem(int position) {
        	     	return cycleValues[position];
        	     	}

        	     	public long getItemId(int position) {
        	     	return 0;
        	     	}

					
					public View getView(int position, View convertView,
							ViewGroup parent) {
						TextView tv = new TextView(context);
						tv.setText(" " + getResources().getText(msgIds[position]));
						tv.setTextSize(24);
                        return tv;
					}
					} );
        
        spnTimeCycle.setOnItemSelectedListener(new OnItemSelectedListener() {
				
				public void onItemSelected(AdapterView<?> parent, View view,
						int position, long id) {
					changeTimeCycleType(position);
					
				}

				
				public void onNothingSelected(AdapterView<?> parent) {
					startDate=endDate=null;
					timeCycleType=TIMERCYCLETYPE_Null;
					refushView();
				}
        	
        });
    }

    
    private void changeTimeCycleType(int posi){
    	switch(posi){
    	    case TIMERCYCLETYPE_Null:
    		     startDate=endDate=null;
    		     timeCycleType=TIMERCYCLETYPE_Null;
    		     refushView();
    		     break;
    	    case TIMERCYCLETYPE_Today:
    	    	fireTodayCycle();
    	    	break;
    	    case TIMERCYCLETYPE_Week:
    	    	fireWeekCycle();
    	    	break;
    	    case TIMERCYCLETYPE_ThreeWeek:
    	    	fireThreeWeekCycle();
    	    	break;
    	    case TIMERCYCLETYPE_Month:
    	    	fireMonthCycle();
    	    	break;
    	    default:
    	}
    }
   

    private void refushView(){
    	btnStartTime.setText(startDate==null?null:dateFormat.format(startDate));
    	btnEndTime.setText(endDate==null?null:dateFormat.format(endDate));
    }
    
    TimeCycleChangedListener l;
    
    public void setTimeCycleChangedListener(TimeCycleChangedListener l){
    	this.l=l;
    }
    
    public void removeTimeCycleChangedListener(){
    	this.l=null;
    }
    
    public void fireTimeCycleChanged(){
    	if(this.l!=null)
    		l.timeCycleChanged(startDate, endDate);
    }
    
    public void fireTodayCycle() {
		startDate=TimeUtil.GetDateNoTime(new Date());
	    endDate=null;	
		timeCycleType=TIMERCYCLETYPE_Today;
		refushView();
	}

	public void fireWeekCycle() {
		startDate=TimeUtil.GetDateNoTime(new Date());
		startDate=TimeUtil.getFirstDateForWeekOfDate(startDate);
	    endDate=null;
		timeCycleType=TIMERCYCLETYPE_Week;
		refushView();
	}

	
	public void fireThreeWeekCycle() {
		
		startDate=TimeUtil.GetDateNoTime(new Date());	
		startDate=TimeUtil.getFirstDateForWeekOfDate(startDate);
		startDate=TimeUtil.addDay(startDate, -14);
		endDate=null;
		timeCycleType=TIMERCYCLETYPE_ThreeWeek;
		refushView();
	}


	public void fireMonthCycle() {
	    startDate=TimeUtil.GetDateNoTime(new Date());	
		startDate=TimeUtil.getFirstDayForMonthOfDate(startDate);
		endDate=null;	
		timeCycleType=TIMERCYCLETYPE_Month;
		refushView();
	}

	public void fireLastCycle() {
		if(startDate==null&&endDate==null)
			return;
		if(timeCycleType==TIMERCYCLETYPE_Null){	
			if(startDate!=null&&endDate!=null){
				int days=TimeUtil.getCalendarDaysAfter((Date)startDate, (Date)endDate);
				startDate=TimeUtil.addDay((Date)startDate, -days);
				endDate=TimeUtil.addDay((Date)endDate, -days);
			}else if(startDate!=null){
				int days=TimeUtil.getCalendarDaysAfter((Date)startDate, new Date());
				endDate=startDate;
				startDate=TimeUtil.addDay((Date)startDate, -days);
			}
		}else if(timeCycleType==TIMERCYCLETYPE_Today){
			int days=1;
			endDate=startDate;
			startDate=TimeUtil.addDay((Date)startDate, -days);
		}else if(timeCycleType==TIMERCYCLETYPE_Week){
			int days=7;
			endDate=startDate;
			startDate=TimeUtil.addDay((Date)startDate, -days);   
		}else if(timeCycleType==TIMERCYCLETYPE_ThreeWeek){
			int days=21;
			endDate=startDate;
			startDate=TimeUtil.addDay((Date)startDate, -days);   
		}else if(timeCycleType==TIMERCYCLETYPE_Month){
			endDate=startDate;
			startDate=TimeUtil.addMonth((Date)startDate, -1);
		}
		refushView();
		fireTimeCycleChanged();
	}

	public void fireNextCycle() {
		if(startDate==null&&endDate==null)
			return;
		if(startDate==null)
			startDate=TimeUtil.GetDateNoTime(new Date());
		if(endDate == null)
			endDate = startDate;
	    if(timeCycleType==TIMERCYCLETYPE_Null){	
	    	if(startDate!=null&&endDate!=null){
				int days=TimeUtil.getCalendarDaysAfter((Date)startDate, (Date)endDate);
				startDate=TimeUtil.addDay((Date)startDate, days);
				endDate=TimeUtil.addDay((Date)endDate, days);
			}else if(endDate!=null){
				int days=TimeUtil.getCalendarDaysAfter((Date)endDate, new Date());
				startDate=endDate;
				endDate=TimeUtil.addDay((Date)endDate, days);
			}
		}else if(timeCycleType==TIMERCYCLETYPE_Today){
			int days=1;
			startDate=endDate;
			endDate=TimeUtil.addDay((Date)endDate, days);
		}else if(timeCycleType==TIMERCYCLETYPE_Week){
			int days=7;
			startDate=endDate;
			endDate=TimeUtil.addDay((Date)endDate, days);   
		}else if(timeCycleType==TIMERCYCLETYPE_ThreeWeek){
			int days=21;
			startDate=endDate;
			endDate=TimeUtil.addDay((Date)endDate, days);   
		}else if(timeCycleType==TIMERCYCLETYPE_Month){
			startDate=endDate;
			endDate=TimeUtil.addMonth((Date)endDate, 1);
		}  
	    refushView();
	    fireTimeCycleChanged();
	}
    
}
